/**
 * 视频管理
 * j
 */
Ext.define('Module.video.VideoManager', {
	extend : 'Ext.panel.Panel',
	layout: 'fit',
	alias: 'widget.videoManager',
	border: false,
	requires: ['Sh.ux.VideoPlayer'],
	initComponent : function() {
		var me = this;
		
		var store = Ext.create('Ext.data.Store',{
			fields: ['id','task_id','bz_id','role_id','stage_id','name',
			         'content','bz_name','bzjs_name','zygc_name','tstart','tend',
			         'starttime','endtime','videotime','status','match','syntime','rawpath','synpath'],
			autoLoad: true,
			proxy: {
				type: 'ajax',
				url: 'videomanager/getVideo.do',
				reader: {
					root: 'datas'
				}
			}
		});
		
		var grid = Ext.create('Ext.grid.Panel',{
			id:me.id+'-vgrid',
			store: store,
			//selModel: Ext.create('Ext.selection.CheckboxModel'),
			tbar: {
				xtype:'form',
				bodyStyle:{'background-color':'rgb(219, 242, 239)'},
				layout: 'absolute',
				height:60,
				items: [{
					x:30,
					y:5,
					xtype: 'textfield',
					fieldLabel: '任务内容',
					labelWidth: 55,
					width: 205,
				},{
					x:298,
					y:5,
					xtype: 'textfield',
					fieldLabel: '班组',
					labelWidth: 32,
					width: 182
				},{
					x:530,
					y:5,
					xtype: 'textfield',
					fieldLabel: '角色',
					labelWidth: 35,
					width: 180
				},{
					x:760,
					y:5,
					xtype: 'textfield',
					fieldLabel: '视频名称',
					labelWidth: 60,
					width: 205
				},{
					x:5,
					y:30,
					xtype: 'datefield',
					format: 'Y-m-d H:i:s',
					submitFormat: 'Y-m-d H:i:s',
					fieldLabel: '录制开始时间',
					labelWidth: 80,
					width: 230,
					value:new Date()
				},{
					x:250,
					y:30,
					xtype: 'datefield',
					format: 'Y-m-d H:i:s',
					submitFormat: 'Y-m-d H:i:s',
					fieldLabel: '录制结束时间',
					labelWidth: 80,
					width: 230,
					value:new Date()
				},{
					x:500,
					y:30,
					xtype:'button',
					id: me.id+'-search',
					text: '查询',
					iconCls: 'icon-search',
					handler: function(){
						var p = this.up('form');
						var startTime = p.down('field[fieldLabel=录制开始时间]').getSubmitValue();
						var endTime = p.down('field[fieldLabel=录制结束时间]').getSubmitValue();
						if(new Date(startTime) > new Date(endTime)){
							Ext.Msg.alert('提示', "巡视开始时间大于巡视结束时间，请重新选择！");
							return;
						}
						grid.store.loadPage(1,{params:{
							taskName:p.down('field[fieldLabel=任务内容]').getValue(),
							bz:p.down('field[fieldLabel=班组]').getValue(),
							js:p.down('field[fieldLabel=角色]').getValue(),
							video:p.down('field[fieldLabel=视频名称]').getValue(),
							startTime:p.down('field[fieldLabel=录制开始时间]').getSubmitValue(),
							endTime:p.down('field[fieldLabel=录制结束时间]').getSubmitValue()
						}});
					}
				},{
					x:560,
					y:30,
					xtype:'button',
					text: '重置',
					handler: function(){
						var p = this.up('form');
						p.down('field[fieldLabel=任务内容]').setValue();
						p.down('field[fieldLabel=班组]').setValue();
						p.down('field[fieldLabel=角色]').setValue();
						p.down('field[fieldLabel=视频名称]').setValue();
						p.down('field[fieldLabel=录制开始时间]').setValue();
						p.down('field[fieldLabel=录制结束时间]').setValue();
					}
				},{
					x:605,
					y:30,
					xtype:'button',
					text: '播放',
					iconCls: 'icon-play',
					handler: function(){
						me.playVideo();
					}
				},{
					x:665,
					y:30,
					xtype:'button',
					text: '关联任务',
					iconCls: 'icon-add',
					handler: function(){
						var rec = grid.getSelectionModel().getSelection()[0];
						if(rec == undefined){
							Ext.Msg.alert('提示','请选择需要与任务关联的视频。');
							return;
						}
						me.videoMacth(rec);
					}
				},{
					x:750,
					y:30,
					xtype:'button',
					text: '删除视频',
					iconCls: 'icon-delete',
					handler: function(b){
						var rec = grid.getSelectionModel().getSelection()[0];
						if(rec == undefined){
							Ext.Msg.alert('提示','请选择需要删除的视频。');
							return;
						}
						
						if(rec.data.content && rec.data.content != ''){
							Ext.Msg.confirm('提示', '视频已关联任务,确定要删除？',function(b){
								if('yes' == b){
									me.deleteVideo(rec.data.id);
								}
							});
						}
						else{
							me.deleteVideo(rec.data.id);
						}
					}
				}],
				listeners:{
					afterrender:function(c){
						c.down('field[fieldLabel=录制开始时间]').setValue();
						c.down('field[fieldLabel=录制结束时间]').setValue();
					}
				}
			},
			bbar: Ext.create('Ext.PagingToolbar', {
				store: store,
				displayInfo: true,
				displayMsg: '显示第 {0} 至 {1} 条记录，共 {2} 条',
	            emptyMsg: "无记录"
			}),
			columns: [{
				text: '视频名称',
				dataIndex: 'name',
				width: 150
			},{
				text: 'ID',
				hidden: true
			},{
				text: '任务',
				columns:[
			         {text: '任务内容',dataIndex: 'content',width: 150,renderer: me.renderMatch},
			         {text: '开始时间',dataIndex: 'tstart',width: 130,renderer: formatDate},
			         {text: '结束时间',dataIndex: 'tend',width: 130,renderer: formatDate}
				],
				dataIndex: 'content',
				width: 150,
				renderer: me.renderMatch
				
			},{
				text: '班组',
				dataIndex: 'bz_name',
				width: 150,
				renderer: me.renderMatch
			},{
				text: '角色',
				dataIndex: 'bzjs_name',
				width: 150,
				renderer: me.renderMatch
			},{
				text: '过程',
				dataIndex: 'zygc_name',
				width: 150,
				renderer: me.renderMatch
			},{
				text: '录制开始时间',
				dataIndex: 'starttime',
				width: 150,
				renderer: formatDate
			},{
				text: '录制结束时间',
				dataIndex: 'endtime',
				width: 150,
				renderer: formatDate
			},{
				text: '视频时长(分)',
				dataIndex: 'videotime',
				width: 150,
				renderer:function(v){
					return timeFormat(v/1000);
				}
			},{
				text: '同步时间',
				dataIndex: 'syntime',
				width: 150,
				renderer: formatDate
			},{
				text: '原始路径',
				dataIndex: 'rawpath',
				hidden: true,
				width: 150
			},{
				text: '路径',
				dataIndex: 'synpath',
				width: 150
			}]
		});
		
		me.items = grid;
		
		me.callParent();
	},
	deleteVideo: function(videoid){
		var me = this;
		Ext.Ajax.request({
			url:'videomanager/deleteVideo.do',
			params:{
				videoid: videoid
			},
			success:function(resp, action){
				var d = Ext.decode(resp.responseText);
				if(d.rst){
					Ext.Msg.alert('提示','删除成功。');
					Ext.getCmp(me.id+'-search').handler();
				}
				else{
					Ext.Msg.alert('提示','删除失败：'+d.msg);
				}
			}
		});
	},
	playVideo: function(){
		var v = this.down('grid').getSelectionModel().getSelection()[0];
		if(v){
			Ext.create('Ext.window.Window',{
				title: '视频播放',
				layout: 'fit',
				width: 800,
				height: 600,
				modal: true,
				items: [{
					xtype: 'player',
					autoPlay:true,
					border: false,
					vdata: v.data,
					src: v.data.synpath,
					match:v.data.match,
					hides: [/*'sign','play','time','full'*/]
				}]
			}).show();
		}
		else{
			Ext.Msg.alert('提示','请选择要播放的视频。');
		}
	},
	renderMatch: function(v){
		if(!v || v == ''){
			return '<font color="blue">无数据</font>';
		}
		else{
			return v;
		}
	},
	videoMacth:function(rec){
		//视频关联
		var me = this;
		var videoStore = Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields:['content','transformer','starttime','endtime','bzname'],
			proxy:{
				type:'ajax',
				extraParams:{task:'worksy'},
				url:'vm/getTaskList.do',
				reader:{
					root: 'datas',
					totalProperty: 'total'
				}
			}
    	});
		
		var _awin = Ext.create("Ext.window.Window",{
			layout:'border',
			width:850,
			height:500,
			modal:true,
			iconCls:'icon-add',
			title:'添加任务关联',
			buttons:[{
				text:'保存',
				handler:function(){
					var thiswin = this.up("window");
					//var bzId = thiswin.down('combobox[name=bzname]').getValue();
					var roleId = thiswin.down('combobox[name=bzjs]').getValue();
					var stageId = thiswin.down('combobox[name=gzjd]').getValue();
					var task = thiswin.down('grid').getSelectionModel().getSelection()[0];
					if(task == undefined){
						Ext.Msg.alert('提示',"请选择要关联的任务。");return;
					}
//					if(bzId == undefined){
//						Ext.Msg.alert('提示',"请选择工作班组");return;
//					}
					if(roleId == undefined){
						Ext.Msg.alert('提示',"请选择班组角色。");return;
					}
					if(stageId == undefined){
						Ext.Msg.alert('提示',"请选工作阶段。");return;
					}
					
					Ext.Ajax.request({
						url:'videomanager/getVideoByTaskid.do',
						params:{
							taskId:task.raw.id,
							roleId:roleId,
							stageId:stageId
						},
						success:function(resp, action){
							var d = Ext.decode(resp.responseText);
							var falg = false;
							var existVid = null;
							for ( var i = 0; i < d.length; i++) {
								if(d[i].bz_id == task.raw.bzid && d[i].role_id == roleId && 
										d[i].stage_id == stageId){
									existVid = d[i].video_id;
									falg = true;
									break;
								}
							}
							var data = {
									bz_id:task.raw.bzid,
									role_id:roleId,
									stage_id:stageId,
									video_id:rec.raw.id,
									task_id:task.raw.id,
									delId:existVid,
									mark:rec.raw.id ? "update" : ""
							};
							if(falg){
								Ext.Msg.confirm('提示', '此任务已关联，点击"确定"将覆盖原数据？',function(x){
									if(x == 'yes'){
										me.vmUpAjax(data, _awin);
									}
								});
							}else{
								me.vmUpAjax(data, _awin);
							}
						}
					});
					
				}
			},{
				text:'取消',
				handler:function(){
					this.up("window").close();
				}
			}],
			items:[{
				region: 'center',
				xtype: 'grid',
//				selModel: Ext.create('Ext.selection.CheckboxModel',{
//					mode:'SINGLE'
//				}),
				tbar:[{
					labelWidth:60,
					fieldLabel: '任务类型',
					name: 'taskType',
					xtype: 'combobox',
					store: {
						xtype: 'store',
						fields: ['v', 'p'],
						data : [
					        {v:'worksy',p: '工作任务'},
					        {v: 'taskxs',p: '巡视任务'}
						]
					},
					value:'工作任务',
					queryMode: 'local',
					displayField: 'p',
					valueField: 'v'
				},{
					labelWidth:65,
					fieldLabel: '任务内容',
					xtype:'textfield'
				},{
					fieldLabel:'开始时间',
					xtype: 'datefield',
					labelWidth: 65,
					width: 170,
					format:'Y-m-d',
//					value:new Date(),
					submitFormat: 'Y-m-d'
				},{
					fieldLabel:'至',
					xtype: 'datefield',
					labelWidth: 20,
					width: 125,
//					value: new Date(),
					format:'Y-m-d',
					submitFormat: 'Y-m-d'
				},{
					text:'查询',
					width: 55,
					iconCls: 'icon-search',
					handler:function(){
						var xgrid = this.up('window').down('grid');
						var taskType = xgrid.down('field[name=taskType]').getSubmitValue();
						xgrid.store.proxy.extraParams = {
								task: taskType == '工作任务'?'worksy':taskType,
								content: xgrid.down('field[fieldLabel=任务内容]').getSubmitValue(),
								startTime: xgrid.down('field[fieldLabel=开始时间]').getSubmitValue(),
								endTime: xgrid.down('field[fieldLabel=至]').getSubmitValue()
						};
						xgrid.store.load();
					}
				}],
				columns:[{
					text:'任务内容',dataIndex:'content',width:250
				},{
					text:'班组',dataIndex:'bzname',width:200
				},{
					text:'开始时间',dataIndex:'starttime',width:130,renderer: formatDate
				},{
					text:'结束时间',dataIndex:'endtime',width:130,renderer: formatDate
				}], 
	        	store:videoStore,
	        	bbar: Ext.create('Ext.PagingToolbar', {
		            store: videoStore,
		            displayInfo: true,
		            displayMsg: '显示第 {0} 至 {1} 条记录，共 {2} 条',
		            emptyMsg: "无记录"
		        }),
	        	listeners:{
//					afterrender:function(){
//						var xgrid = this.up('window').down('grid');
//						xgrid.down('field[fieldLabel=任务名称]').setValue('');
//						xgrid.down('field[fieldLabel=开始时间]').setValue('');
//						xgrid.down('field[fieldLabel=至]').setValue();
//					}
				}
			},{
				region: 'south',
				xtype:'form',
				layout:'column',
				bodyPadding: 10,
				border: false,
				defaults: {padding:'0 20 0 0',allowBlank: false,columnWidth:1/3,labelWidth:60},
				items:[/*{
					xtype:'combobox',
					name:'bzname',
					fieldLabel: '工作班组',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						//autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							//extraParams:{bzId:rec.raw.bzid},
							url: 'worksy/getBz.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							var gridCel = c.up('window').down('grid');
							c.addManagedListener(gridCel,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzId: gridCel.getSelectionModel().getSelection()[0].raw.bzid
								}});
							});
						}
					}
				},*/{
					xtype:'combobox',
					name:'bzjs',
					fieldLabel: '班组角色',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							url: 'vm/getBzRole.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							/*var bzname = c.up('form').down('combobox[name=bzname]');
							c.addManagedListener(bzname,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzId: bzname.getSubmitValue()
								}});
							});*/

							var gridCel = c.up('window').down('grid');
							c.addManagedListener(gridCel,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzId: gridCel.getSelectionModel().getSelection()[0].raw.bzid
								}});
							});
						
						}
					}
				},{
					xtype:'combobox',
					name:'gzjd',
					fieldLabel: '工作阶段',
					readOlay:true,
					displayField: 'name',
					valueField: 'id',
					queryMode: 'local',
					store: Ext.create('Ext.data.Store',{
						//autoLoad: true,
						fields: ['id','name'],
						proxy: {
							type: 'ajax',
							extraParams:{rwid: rec.raw.id},
							url: 'vm/getGc.do'
						}
					}),
					listeners:{
						afterrender:function(c){
							/*var bzname = c.up('form').down('combobox[name=bzname]');
							c.addManagedListener(bzname,'select',function(){
								c.setValue();
								c.store.load({params:{
									bzid: bzname.getSubmitValue()
								}});
							});*/
							

							var gridCel = c.up('window').down('grid');
							c.addManagedListener(gridCel,'select',function(){
								c.setValue();
								c.store.load();
							});
						
						}
					}
				}]
			}]
		}).show();
	},
	vmUpAjax:function(data,_awin){
		var me = this;
		Ext.Ajax.request({
			url:'videomanager/addTaskMatch.do',
			params:data,
			success:function(resp){
				if(resp.responseText == 1){
					Ext.Msg.alert("提示","关联任务成功。");
					Ext.getCmp(me.id+'-vgrid').store.reload();
					_awin.close();
				}
				else{
					Ext.Msg.alert("提示","关联任务失败。");
				}
			}
		});
	}
});